<template>
  <div id="qualifyReport">
    <div class="bg-color-black">
      <div class="d-flex pt-2 pl-2">
        <dv-decoration-8 style="width:25%;height:40px;" />
        <span class="fs-xl text mx-2" style="width:50%;height:40px;font-size:30px;display: inline-block;line-height: 40px;text-align: center">上一班组原片优化</span>
        <dv-decoration-8 :reverse="true" style="width:25%;height:40px;" />
      </div>
      <div style="margin-top: 10px">
        <dv-scroll-board :config="config" style="width:580px;height:380px" />
      </div>
    </div>
  </div>
</template>

<script>
import {yieldCalYield, yieldOpStaticList} from '@/api/yield'
import dayjs from 'dayjs'
import customParseFormat from 'dayjs/plugin/customParseFormat'

dayjs.extend(customParseFormat)

export default {
  data() {
    return {
      timing2: null,
      config: {},
      rowNum: 3,
      currentFinalData: []
    }
  },
  computed: {},
  watch: {
    currentFinalData: {
      handler(newdata) {
        this.config = {
          // 表头背景色 '#00BAFF''#003B51''#0A2732'
          headerBGC: '#00BAFF75',
          oddRowBGC: '#003B5145',
          evenRowBGC: '#0A273245',
          header: ['线', '长*宽*厚度', '质量等级', '片数', '面积(㎡)'],
          // header: ['线-时间', '良品面积', '良品率', '引出量T/D'],
          // header: ['线-时间', '良面积', '良率', '溶解不良率', '压延不良率'],
          // header: ['成品', '片数', '重箱', '占比%'],
          // index: true,
          headerHeight: 50,
          columnWidth: [60, 190, 110, 110, 120],
          align: ['center', 'center', 'center', 'center', 'center'],
          // 轮播方式 'single'|'page'
          carousel: 'page',
          // 轮播时间间隔 2000
          waitTime: 10000,
          rowNum: this.rowNum,
          data: newdata
        }
      },
      immediate: true
      // deep: true
    }
  },
  mounted() {
    this.getYieldCalTableNew()
    this.timeFn2() // todo base
  },
  beforeDestroy() {
    clearInterval(this.timing2)
  },
  methods: {
    // 特殊表的合成方式
    getYieldCalTableNew() {
      const queryStaticsParams = {}
      let line24Statics = []
      line24Statics = ['20250607080000', '20250607195959'] // 测试时间 实际数据为06b
      const currentTime = new Date()
      // const yieldSort = 'DESC'
      const yieldSort = 'ASC'
      const thisTime = Number(dayjs(currentTime).format('HH'))
      if (thisTime >= 8 && thisTime < 20) {
        // 此时此刻大于等于8点 小于20点 则查昨晚晚班
        // 昨晚晚班 即今日早班creat
        line24Statics[0] = dayjs(currentTime).format('YYYYMMDD') + '080000' // 实际时间
        line24Statics[1] = dayjs(currentTime).format('YYYYMMDD') + '175959' // 实际时间
      } else if (thisTime < 8) {
        // 此时此刻小于8点 则查昨日白班
        // 昨日白班 即昨晚晚班creat
        line24Statics[0] = dayjs(currentTime).subtract(1, 'day').format('YYYYMMDD') + '200000' // 实际时间
        line24Statics[1] = dayjs(currentTime).format('YYYYMMDD') + '075959' // 实际时间
      } else if (thisTime >= 20) {
        // 大于等于20点 则查今日白班
        // 今日白班 即今日晚班creat
        line24Statics[0] = dayjs(currentTime).format('YYYYMMDD') + '200000' // 实际时间
        line24Statics[1] = dayjs(currentTime).format('YYYYMMDD') + '235959' // 实际时间
      }
      // line24Statics = ['20250607080000', '20250607195959'] // 测试时间 实际数据为06b todo 生产端需注释
      yieldOpStaticList(this.addParam((this.addDateRange(queryStaticsParams, [line24Statics[0], line24Statics[1]])), 'sort', yieldSort)).then(response => {
        let countMax = 0
        let currentCount = 0
        const yieldOpStaticArr = response.data.reduce((acc, cur) => {
          currentCount = acc[Number(cur.line) - 1].length - countMax
          currentCount > 0 ? countMax = countMax + currentCount : countMax
          acc[Number(cur.line) - 1].push([this.sumCell(cur.line), this.formatCell(cur.specCode), cur.level, cur.qty, Number(cur.square.toFixed(0))])
          // ['一线', '2274*1130*3*6', 6, 1, 2.57],
          // ['四线', '合计：', '', 10128, 26042.65], // todo 合计后续补完！！！
          return acc
        }, [[], [], [], [], []])
        countMax = countMax + 1
        console.log('yieldOpStaticArr', countMax, yieldOpStaticArr)
        // 数据为1-5线排列 1 找出单线最大值 形成空数组 填值
        const emptyArr = ['', '', '', '', '']
        for (let i = 0; i < yieldOpStaticArr.length; i++) {
          for (let j = 0; j < yieldOpStaticArr[i].length; j++) {
            this.currentFinalData.push(yieldOpStaticArr[i][j])
          }
          countMax = 5 // 3行有点难看 默认开5行
          if (yieldOpStaticArr[i].length < countMax) {
            console.log('触发增补')
            for (let k = 0; k < countMax - yieldOpStaticArr[i].length; k++) {
              this.currentFinalData.push(emptyArr)
            }
          }
        }
        this.rowNum = countMax
        // console.log(this.currentFinalData)
      })
    },
    timeFn2() {
      this.timing2 = setInterval(() => {
        console.log('t3 right 实时2', dayjs(new Date()).format('YYYYMMDDHHmmss'))
        this.getYieldCalTableNew()
      }, 120000)
    },
    // 单元格处理 去掉*等级
    formatCell(specCode) {
      const strArr = specCode.split('*')
      specCode = strArr[0] + '*' + strArr[1] + '*' + strArr[2]
      return specCode
    },
    // 合计单元格处理
    sumCell(val) {
      if (val !== '合计') {
        return val + '线'
      } else {
        return '合计'
      }
    }
  }
}
</script>

<style lang="scss" scoped>
  $box-height: 485px;
  $box-width: 100%;
  #qualifyReport {
    padding: 20px 16px 20px 16px;
    height: $box-height;
    width: $box-width;
    border-radius: 5px;

    .bg-color-black {
      padding: 5px;
      height: $box-height - 35px;
      width: $box-width;
      border-radius: 10px;
    }

    .text {
      color: #c3cbde;
    }

    .chart-box {
      margin-top: 16px;
      width: 170px;
      height: 170px;

      .active-ring-name {
        padding-top: 10px;
      }
    }
  }
</style>
